package com.zhike.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class AccountNumberUtils {

    /**
     * 随机生成6位数字（包括字母和数字）
     * 可用于生成验证码
     *
     * @return 结果
     */
    public static String createAccountNumber() {
        // 生成一个由大写字母和数字组成的6位随机字符串，并且字符串不重复
        char[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        boolean[] flags = new boolean[letters.length];
        char[] cha = new char[6];
        for (int i = 0; i < cha.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            } while (flags[index]);//判断生成的字符串是否重复
            cha[i] = letters[index];
            flags[index] = true;
        }

        String res = String.valueOf(cha);
        log.info("生成六位随机账号：" + res);
        return res;
    }


    /**
     * 生成6位随机验证码
     */
    public static String generateCode6() {
        Random randObj = new Random();
        return Integer.toString(100000 + randObj.nextInt(900000));
    }

    /**
     * 生成4位随机验证码
     */
    public static String generateCode4() {
        Random randObj = new Random();
        return Integer.toString(1000 + randObj.nextInt(9000));
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            createAccountNumber();
        }
    }
}
